package cn.wolfcode.mq.linstener;

import cn.wolfcode.domain.SeckillProduct;
import cn.wolfcode.mq.MQConstant;
import cn.wolfcode.mq.OrderMQResult;
import cn.wolfcode.redis.CommonRedisKey;
import cn.wolfcode.redis.SeckillRedisKey;
import cn.wolfcode.service.ISeckillProductService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.annotation.SelectorType;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @Author: 普发忠
 * @Date: 2021/11/11/14:17
 */
@Component
@RocketMQMessageListener(
        topic = MQConstant.ORDER_RESULT_TOPIC,
        consumerGroup = "order_result_topic-fall-consumer",
       selectorType = SelectorType.TAG,
        selectorExpression=MQConstant.ORDER_RESULT_FAIL_TAG
)
public class OrderResultForFallListener implements RocketMQListener<OrderMQResult> {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private ISeckillProductService seckillProductService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    
    @Override
    public void onMessage(OrderMQResult ret) {
//        同步预库存
        String bigkeystory = CommonRedisKey.SECKILL_PRODUCT_STORE.getRealKey(String.valueOf(ret.getTime()));
        SeckillProduct seckillProduct = seckillProductService.findfromDB(ret.getSeckillId());
        redisTemplate.opsForHash().put(bigkeystory, String.valueOf(ret.getSeckillId()),
                String.valueOf(seckillProduct.getStockCount()));
        rocketMQTemplate.syncSend(MQConstant.CANCEL_SECKILL_PERSION,ret);
        //删除秒杀者
        rocketMQTemplate.syncSend(MQConstant.CANCEL_SECKILL_PERSION,ret);
//        广播本地标识,修改为true.
        rocketMQTemplate.syncSend(MQConstant.CANCEL_SECKILL_OVER_SIGE_TOPIC,ret.getSeckillId());
        
    }
}
